/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package DataEvaluation;

import java.util.ArrayList;

/**
 *
 * @author Manuel
 */
public class CommandCreator {

    public ArrayList<String[]> getResultCommands(ArrayList<String[][]> pairs, String dir) {
        ArrayList<String[]> commands = new ArrayList<String[]>();

        for(int j = 0;j<pairs.size();j++){

            String[][] currentPair = pairs.get(j);

            //if one of the two properties is on class level, apply clustering
            if(currentPair[1][0].contains("*c")){
                String[] currentCommand = new String[2];
                //get the names of the properties
                String namex = currentPair[0][0];
                String namey = currentPair[1][0].replace("*c", "");
                //first set the working dir
                String workdir = "setwd(\""+dir+"\");";

                //second fill vectors
                String inds = "inds <- c(";
                String mins = "mins <- c(";
                String maxs = "maxs <- c(";
                String avgs = "avgs <- c(";
                for(int i = 1;i<currentPair[0].length;i++){
                    String dep = currentPair[1][i];
                    dep = dep.replace("(", "").replace(")","");
                    String[] vals = dep.split(",");
                    if(vals.length == 3){
                        avgs+=vals[0];
                        mins+=vals[1];
                        maxs+=vals[2];
                        inds += (currentPair[0][i]);
                    }

                    if(i != (currentPair[0].length-1)){
                        inds += ",";
                        mins += ",";
                        maxs += ",";
                        avgs += ",";
                    }
                }
                inds += ");";
                mins += ");";
                maxs += ");";
                avgs += ");";

                String[] xband = this.getBands(currentPair[0]);
                String[] yband = this.getBands(currentPair[1]);
                String genCommand = workdir+inds+mins+maxs+avgs;
                String plotFile = "plot"+j+".jpg";
                String plotCmd = "jpeg(\""+plotFile+"\");plot.new(); plot.window(xlim=c("+xband[0]+","+xband[1]+"), ylim=c("+yband[0]+","+yband[1]+"));points(inds,mins,pch=20,col=\"red\");"
                        + "points(inds,avgs,pch=20,col=\"blue\");points(inds,maxs,pch=20,col=\"green\");axis(1);axis(2);box();title(xlab=\""+namex+"\",ylab=\""+namey+"(min,avg,max)\",main=\""+namex +","+namey+"\");";
                
                currentCommand[0]=genCommand + plotCmd;
                currentCommand[1]=plotFile;
                commands.add(currentCommand);

            }

            //else, if they are both on project level, do regression and correllation analysis
            else{
                String[] currentCommand = new String[4];
                //get the names of the properties
                String namex = currentPair[0][0];
                String namey = currentPair[1][0];

                //first set the working dir
                String workdir = "setwd(\""+dir+"\");";

                //second fill two vectors
                String vec1 = namex+" <- c(";
                String vec2 = namey+" <- c(";
                for(int i = 1;i<currentPair[0].length;i++){
                    vec1 += (currentPair[0][i]);
                    vec2 += (currentPair[1][i]);
                    if(i != (currentPair[0].length-1)){
                        vec1 +=",";
                        vec2 +=",";
                    }
                }
                vec1 += ");";
                vec2 += ");";
                String regCmd = "regression<-summary(lm("+namey+"~"+namex+"));cat(makejava(regression));";
                String corCmd = "correlation<-as.character(cor.test("+namex+","+namey+"));cat(makejava(correlation));";
                String plotFile = "plot"+j+".jpg";
                String plotCmd = "jpeg(\""+plotFile+"\");plot.new();plot("+namex+","+namey+",main=\""+namex +","+namey+"\",pch=20);abline(lm("+namey+"~"+namex+"));";
                String genCommand = workdir+vec1+vec2;
                currentCommand[0]=genCommand+corCmd;
                currentCommand[1]=genCommand+regCmd;
                currentCommand[2]=genCommand+plotCmd;
                currentCommand[3]=plotFile;
                commands.add(currentCommand);

            }
        }

        return commands;
    }

    public ArrayList<String[]> getInterMediateCommands(ArrayList<String[]> columns, String dir) {
        ArrayList<String[]> commands = new ArrayList<String[]>();

        for(int j = 0;j<columns.size();j++){

            String[] currentColumn = columns.get(j);

            //if one of the two properties is on class level, apply clustering
                String[] currentCommand = new String[4];
                //get the names of the properties
                String name = currentColumn[0].replace("*c", "");
                //first set the working dir
                String workdir = "setwd(\""+dir+"\");";

                //second fill vector
                String vals = name+" <- c(";
                for(int i = 1;i<currentColumn.length;i++){
                    String current = currentColumn[i];
                    if(current.contains("(") && current.contains(")"))
                        vals += current.substring(current.indexOf("(")+1,current.indexOf(","));

                     else
                         vals += current;
                    if(i != currentColumn.length-1)
                        vals+=",";
                }
                vals += ");";
                String genCommand = workdir+vals;
                String analysisCommand = "analysis <- as.character(c(summary("+name+"),sd("+name+")));cat(makejava(analysis));";
                String plotFile = "interplot"+j+".jpg";
                String plotCmd = "jpeg(\""+plotFile+"\");hist("+name+",main=\"Histogram of "+name+"\");";

                currentCommand[0]=genCommand + analysisCommand;
                currentCommand[1]=genCommand + plotCmd;
                currentCommand[2]=name.replace("*c", "");
                currentCommand[3]=plotFile;
                commands.add(currentCommand);

        }

            
        return commands;
    }

    
    private String[] getBands(String[] values) {
        String[] bands = new String[2];
        double minval = 0;
        double maxval = -10000000;
        for(int i = 1;i<values.length;i++){
            String triple = values[i];
            if(triple.contains("(") && triple.contains(")")){
                String[] vals = triple.substring(1,triple.indexOf(")")).split(",");
                double cmin = Double.valueOf(vals[1]);
                double cmax = Double.valueOf(vals[2]);
                if(cmin<minval)
                    minval = cmin;
                if(cmax>maxval)
                    maxval = cmax;
            }
            else{
                double no = Double.valueOf(triple);
                if(no>maxval)
                    maxval = no;
                if(no<minval)
                    minval = no;
            }

        }
        maxval = maxval + (maxval*6/100);
        minval = minval - (minval*10/100);
        bands[0] = Double.toString(minval);
        bands[1] = Double.toString(maxval);
        return bands;
    }



}
